#include "chatbubble.h"
#include <QEvent>
#include <QMouseEvent>

ChatBubble::ChatBubble(bool isSender, ChatWidget* prt, QWidget *parent)
    : QWidget(parent), isSender(isSender),prt(prt)
{


    // 头像
    //    QLabel* head = new QLabel(this);
    //    head->setFixedSize(50, 50);;
    // 信号上传


    // 区分发送方和接收方
    if(isSender){
        color = QColor(144, 238, 144);
        mg = QMarginsF(12, 10, 15, 10);
        // 头像
        QPixmap senderHead(":/new/prefix1/icon/001.png");
        //        head->setPixmap(senderHead);
    }
    else{
        color = QColor(234, 234, 234);
        mg = QMarginsF(15, 10, 12, 10);
        QPixmap receiverHead(":/new/prefix1/icon/001.png");
        //        head->setPixmap(receiverHead);
    }

    //    QPalette pal(palette());
    //    pal.setColor(QPalette::Window, color);

    // 设置边框
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setContentsMargins(mg.left(), mg.top(), mg.right(), mg.bottom());

    txt = new QLabel();
    // txt->setGeometry(QRect(25, 2, 75, 35));
    txt->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);  // 设置大小策略
    txt->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);  // 设置对齐方式
    txt->setWordWrap(true);
    txt->setOpenExternalLinks(true);
    // 消息框大小
    txt->setMaximumWidth(600);
    txt->adjustSize();

    // 字体
    QFontMetrics font(txt->font());
    //    QSize size(font.width(msg.msg), font.height());
    QSize size(txt->width(), txt->height());

    setMinimumSize(size.width() + mg.left() + mg.right(),
                   size.height() + mg.top() + mg.bottom());

    layout->addWidget(txt);
    // 头像和消息并列
    //    QHBoxLayout* headAndMessage = new QHBoxLayout;
    //    if(isSender){
    //        headAndMessage->addWidget(head);
    //     qDebug() << "sender!";
    //    }

    //    QWidget* www = new QWidget;
    //    www->setLayout(layout);
    //    headAndMessage->addWidget(www);

    //    if(!isSender){
    //        headAndMessage->addWidget(head);
    //    }

    //    this->setLayout(headAndMessage);
    //    layout->setSpacing(15);

}

void ChatBubble::paintEvent(QPaintEvent* event) {
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    QPainterPath path;

    path.addRoundedRect(rect(), mg.left(), mg.top());
    QPen pen(color);
    QBrush brush(color);

    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawPath(path.simplified());
}

void ChatBubble::set_txt(QString send_txt){
    txt->setText(send_txt);

    //    if (!send_txt.isEmpty()) {
    //        QPainter painter(this);
    //        painter.setRenderHint(QPainter::Antialiasing);

    //        QPainterPath path;

    //        path.addRoundedRect(rect(), mg.left(), mg.top());
    //        QPen pen(color);
    //        QBrush brush(color);

    //        painter.setPen(pen);
    //        painter.setBrush(brush);
    //        painter.drawPath(path.simplified());
    //    }
}
void ChatBubble::set_file(const QString& fileName,const QByteArray& info,const int& sz){
    file_info = info;
    r_sz = sz;
    txt->setText(fileName);
    txt->installEventFilter(this);    // 安装事件过滤器
}
void ChatBubble::set_picture(QPixmap picture){
    txt->setPixmap(picture);
}

bool ChatBubble::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == txt){
        //单击
        if (event->type() == QEvent::MouseButtonPress) {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            // 在左键点击时，选择是否保存文件
            if (mouseEvent->button() == Qt::LeftButton) {
                QWidget* new_widget = new QWidget;
                // 创建垂直布局
                QVBoxLayout *layout = new QVBoxLayout;

                // 创建标签显示提示信息
                QLabel *label = new QLabel("是否接收文件？");
                layout->addWidget(label);

                // 创建两个按钮
                acceptButton = new QPushButton("是");
                QPushButton *rejectButton = new QPushButton("否");

                connect(acceptButton, &QPushButton::clicked,this,&ChatBubble::downloadFile);
                connect(this, &ChatBubble::ser_downloadFile,prt,&ChatWidget::receiveFile_CB2C);
                connect(acceptButton, &QPushButton::clicked,new_widget,&QWidget::close);
                connect(rejectButton, &QPushButton::clicked,new_widget,&QWidget::close);
                //connect(acceptButton, &ChatBubble::mouseReleaseEvent, );

                // 将按钮添加到布局中
                layout->addWidget(acceptButton);
                layout->addWidget(rejectButton);

                // 将布局设置给主窗口
                new_widget->setLayout(layout);

                // 显示主窗口
                new_widget->show();
            }
        }
        else{
            return false;
        }
    }
    return QWidget::eventFilter(obj, event);
}
void ChatBubble::downloadFile(){
    QString filename = txt->text();
//这里需要转入文件更多的信息：文件大小,和文件MID
    emit ser_downloadFile(filename,file_info,r_sz);
}
